package com.lam.mall.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.common.pay.config.DvyConfig;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.group.Add;
import com.lam.common.validate.group.Update;
import com.lam.common.validate.model.ValidOperType;
import com.lam.framework.annotation.ParamInject;
import com.lam.framework.operLog.annotation.Log;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.mall.common.constant.MallConstant;
import com.lam.mall.entity.Delivery;
import com.lam.mall.param.DeliveryParam;
import com.lam.mall.service.DeliveryService;

/**
 * 物流公司Controller
 * 
 * @author admin
 * @date 2023-04-27
 */
@Log(title = "物流公司")
@ParamInject
@RestController
@RequestMapping("/${adminPath}/mall/delivery")
public class DeliveryController extends BaseController {

    @Autowired
    private DeliveryService deliveryService;

    /**
     * 查询物流公司列表
     */
    @Log(oper="分页查询")
    @PreAuthorize("@ss.hasPm('mall:delivery:list')")
    @GetMapping("/list")
    public Result list(DeliveryParam param) {
    
        startPage();
        List<Delivery> list = deliveryService.selectList(param);
        for (Delivery delivery : list) {
        	delivery.setQueryUrl(DvyConfig.getDvyUrl("运单号", delivery.getCode()));
		}
        return Result.ok(getTableData(list));
    }
    
    /**
     * 获取物流公司详细信息
     */
    @Log(oper="根据ID查询")
    @PreAuthorize("@ss.hasPm('mall:delivery:query')")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") String id) {
    
        return Result.ok(deliveryService.selectById(id));
    }
    
    /**
     * 获取物流公司详细信息
     */
    @Log(oper="根据ID查询")
    @PreAuthorize("@ss.hasPm('mall:delivery:query')")
    @GetMapping("/dvyInfo")
    public Result getDvyInfo(@RequestParam("id") String id, @RequestParam("dvyFlowNo") String dvyFlowNo) {
    
    	Delivery info = deliveryService.selectById(id);
    	if(info != null) {
    		info.setQueryUrl(DvyConfig.getDvyUrl(dvyFlowNo, info.getCode()));
    	}
        return Result.ok(info);
    }

    /**
     * 新增物流公司
     */
    @PreAuthorize("@ss.hasPm('mall:delivery:add')")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated(Add.class) @RequestBody Delivery delivery) {
    
        ValidateUtil.validUnique(delivery, ValidOperType.ADD, MallConstant.UNIQUE_DELIVERY_DVY_NAME, "物流公司名称已存在！");
        ValidateUtil.validUnique(delivery, ValidOperType.ADD, MallConstant.UNIQUE_DELIVERY_CODE, "公司编码已存在！");
        return toAjax(deliveryService.insert(delivery));
    }

    /**
     * 修改物流公司
     */
    @PreAuthorize("@ss.hasPm('mall:delivery:edit')")
    @Log(businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@Validated(Update.class) @RequestBody Delivery delivery) {
    
        ValidateUtil.validUnique(delivery, ValidOperType.UPDATE, MallConstant.UNIQUE_DELIVERY_DVY_NAME, "物流公司名称已存在！");
        ValidateUtil.validUnique(delivery, ValidOperType.UPDATE, MallConstant.UNIQUE_DELIVERY_CODE, "公司编码已存在！");

        return toAjax(deliveryService.update(delivery));
    }

    /**
     * 删除物流公司
     */
    @PreAuthorize("@ss.hasPm('mall:delivery:remove')")
    @Log(businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable String id) {
    
        return toAjax(deliveryService.deleteById(id));
    }

}
